layui.use(['element','laydate','form'], function(){

    var laydate = layui.laydate;
    laydate.render({
        elem: '#birthday'
        ,done: function(value, date, endDate){
            console.log(date);
            var birthday=new Date()
            birthday.setFullYear(date.year,date.month,date.date)
            vm.clientBirthday =birthday
        }
    });

    var orderId = location.hash.substring(1);
    vm.orderId = orderId;

    var form = layui.form;
    setTimeout(function (){
        form.render('select');
    }, 500);

    // form.on('select(product)', function(data){
    //     vm.productId = data.value;
    //     for(i=0;i<vm.productList.length;i++){
    //         if (vm.productList[i].productId === Number(vm.productId)) {
    //             vm.orderDetailPrice =  Number(vm.productList[i].productRetailPrice)*Number(vm.productSize);
    //         }
    //     }
    // });

    form.on('select(client)', function(data){
        vm.companyId = data.value;
    });

});

var vm = new Vue({
    el: '#app',
    data: {
        companyId : 1,
        productId : 1,
        productList : [],
        clientList : [],
        productSize: 0,
        orderDetailPrice: 0,
        orderId : 0
    },
    created() {
        this.getAllProduct();
        this.getAllCompany();
    },
    methods: {
        getAllProduct(){
            $.ajax({
                type: "get",
                url: "/copProduct/list",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        vm.productId = result.data[0].productId;
                        vm.productList = result.data;
                    }
                }
            });
        },
        getAllCompany(){
            $.ajax({
                type: "get",
                url: "/copClient/list",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        console.log(result);
                        vm.companyId = result.data[0].companyId;
                        console.log(vm.companyId)
                        vm.clientList = result.data;
                    }
                }
            });
        },
        add() {
            var orderTmp = {
                'orderCompanyId': vm.companyId,
                'orderTotalPrice': vm.orderDetailPrice,
                'productId': vm.productId,
                'productSize': vm.productSize,
                'orderDetailPrice': vm.orderDetailPrice,
                'orderStatus': 1
            };
            var orderData = JSON.stringify(orderTmp)
            $.ajax({
                type: "post",
                url: "/copOrder",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: orderData,
                success: function (result) {
                    console.log(result)
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        alert("成功")
                        window.location.href="OrderDetail.html?#"+result.data
                    }
                }
            });
        },
        back(){
            window.location.href="OrderDetail.html?#"+vm.orderId
        }
    }
})